<?php
class Common{
  public $debug = 1; // 0=>Off , 1=>On
  public $debugData = array();

  function __construct()
  {
    date_default_timezone_set('Asia/Kolkata');
  }

  function __destruct()
  {
    echo "<h1>Anikesh Calling .. Anikesh ..</h1>";
  }

  /***********************************
   * Alternative of print_r()
   ***********************************/
  function pr($data)
  {
    $this->debugData;

    $message =  '<pre style="color:#fff;background:#000;font-size:20px;">';
    $message .= print_r($data,1);
    $message .= '</pre>';
    array_push($this->debugData, $message);

  }
  function pr1($data)
  {
    $this->debugData;
      $message = '';
      $message .= '<pre style="color:red;background:yellow;font-size:20px;">';
      $message .= print_r($data,1);
      $message .= '</pre>';
      array_push($this->debugData, $message);

  }

  function vd($data)
  {

     $this->debugData;
      $message = '';
    $message .= '<pre style="color:red;background:yellow;font-size:20px;">';
    $message .= var_export($data,1);
    $message .= '</pre>';
    array_push($this->debugData, $message);

  }

  function getStatus($status=0) {
    return  $status == 0 ? '<i class="icon-star-empty icon-2x" ></i>' : '<i class="icon-star icon-2x" ></i>';
  }


  function success($msg='')
  {
    return "<div class=\"alert alert-success alert-dismissable\">  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button><strong>$msg</strong></div>";
  }

  function error($msg='')
  {
    return "<div class=\"alert alert-danger alert-dismissable\">  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button><strong>$msg</strong></div>";
  }

  function redirect($page='')
  {
    header("location:$page");
    exit;
  }


  /***
   * CODE TO UPLOAD FILE
   * @params $files ARRAY "Same as $_FILES"
   * @params $name STRING "Name of Uploaded Field"
   * @params $path STRING "Where you need to upload"
   * */
  function upload($files, $name='',$path='')
  {
    if(empty($files) || $name=='') {
      return false;
    }

    #sanitize Path
    $pathlen = strlen($path);
    if($path[$pathlen-1] != '/') {
      $path = "{$path}/";// to avoid path error add slash in last
    }

    $allowedSize = 1; //MB
    $allowedFileSize = $allowedSize * 1024 * 1024 ; // Bytes
    $allowedExt = array('jpg','jpeg','png','bmp');
    $extns = implode(',',$allowedExt);
    $p_image = '';
    $flag = '';
    $msg = '';

    if($files[$name]['error']==0)
    {

      if($files[$name]['size'] <= $allowedFileSize)
      {
        $p_image = $files[$name]['name'];
        $p_image = date('ymdHis').'_'.uniqid().'_'.$p_image;
        $fileInfoArray = pathinfo($p_image);
        $fileExt = $fileInfoArray['extension'];



        if(in_array($fileExt,$allowedExt))
        {
          $src = $files[$name]['tmp_name'];
          $dest = $path.$p_image;
          $isMoved = @move_uploaded_file($src, $dest);


          if($isMoved == true) {
            $flag = 1;
          } else {
            $flag = 0;
            $msg =  "Uploads folder misssing or there may be write permission issue. ";
          }

        } else {
          $flag = 0;
          $msg =  "Please upload file with valid extn ($extns)";
        }

      } else {
        $flag = 0;
        $msg =  "Please upload file with MAX $allowedSize MB";
      }
    } else {
      $flag = 0;
      $msg = "Error while Uploading File Size Exceed to 2MB";

    }

    return array(
      'file_new_name'=>$p_image,
      'flag'=>$flag,
      'msg'=>$msg
    );
  }//end upload()




  function sendMail($to,$subject,$message)
  {

    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= 'To: Mary <nb158f@gmail.com>' . "\r\n";
    $headers .= 'From: Birthday Reminder <nb158f@gmail.com>' . "\r\n";
    //$headers .= 'Reply-To: anujain@gmail.com' . "\r\n";
    $headers .= 'Cc: vikas.nice@gmail.com' . "\r\n";
    //$headers .= 'Bcc: vikas@gmail.com' . "\r\n";

    // Mail it
    $flag = mail($to, $subject, $message, $headers);

    return $flag;


  }

  function sendActivationMail($email='',$id)
  {
    $id = encrypt($id);
    $to      = 'nb158f@gmail.com';
    $subject = 'Please verify your email for mysite.com';
    $message = 'hello please click the link below..';
    $message .= "<a href=\"http://localhost/phpmb126/try/activate.php?id=$id\">http://localhost/nb158g/admin/activate.php?id=$id</a>";
    return $this->sendMail($to,$subject,$message);
  }


  /*
   * File Contain two functions encrypt() and decrypt()
   * used to encode and decode strings with the help of user defined salt
   * */

  function encrypt($string='')
  {
    // This is 32 bit salt value change it
    $salt = '9iwjwj455aufj4669gkfmdfkl244h45t';

    $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256,
                                              md5($salt),
                                              $string,
                                              MCRYPT_MODE_CBC,
                                              md5(md5($salt))
                                             )
                              );

    return urlencode($encrypted);
  }

  function decrypt($encrypted='')
  {
    $encrypted = urldecode($encrypted);
    // This is 32 bit salt value change it
    $salt = '9iwjwj455aufj4669gkfmdfkl244h45t';

    $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256,
                                      md5($salt),
                                      base64_decode($encrypted),
                                      MCRYPT_MODE_CBC,
                                      md5(md5($salt))
                                      ),
                  "\0");
    return $decrypted;
  }

}// end Common
